package com.example.easyutils.food;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintSet;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.easyutils.R;
import com.example.easyutils.util.MyRecycleViewAdapter;
import com.example.easyutils.watermark.ClearWatermark;
import com.example.easyutils.watermark.Vendor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;

//recycleView
public class FoodRecycleView extends RecyclerView.Adapter {

    private List list;
    private Context context;
    public FoodRecycleView(List mList, Context context) {
        this.list = mList;
        this.context = context;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cook_item,
                parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ViewHolder) {
            Map map = ((Map)list.get(position));
            String title = map.get("name").toString(),content = map.get("content").toString(),
            time = map.get("cookingtime").toString(),tag = map.get("tag").toString();

            List processList = (List) map.get("process"),materialList = (List) map.get("material");
            ((ViewHolder) holder).mTitle.setText(title);
            ((ViewHolder) holder).mContent.setText(content);
            ((ViewHolder) holder).mTime.setText(time);
            ((ViewHolder) holder).mTag.setText(tag);
//            ((ViewHolder) holder).mNeed.setText(materialList.get(position));
            String needName = null,needAmount = null,neeStr = "";
            for(int i = 0; i < materialList.size(); i ++){
                needName = ((Map)materialList.get(i)).get("mname").toString();
                needAmount = ((Map)materialList.get(i)).get("amount").toString();
                neeStr += i+1+"、"+needName+":"+needAmount+"；\n ";
            }
            ((ViewHolder) holder).mNeed.setText("准备材料："+neeStr);

//            RecyclerView recyclerView = new RecyclerView(context);
            Log.d("processList",position+"");
             StepRecycleView myRecycleViewAdapter;//适配器
             LinearLayoutManager mLinearLayoutManager;//布局管理器
            mLinearLayoutManager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
            myRecycleViewAdapter = new StepRecycleView(processList,context);
            ((ViewHolder) holder).mrecyclerView.setLayoutManager(mLinearLayoutManager);
            ((ViewHolder) holder).mrecyclerView.setAdapter(myRecycleViewAdapter);

        }
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mContent,mTime,mTag,mNeed,mTitle;
//        public ImageView mStepImage;
        public RecyclerView mrecyclerView;
        public ViewHolder(View view) {
            super(view);
            mContent = view.findViewById(R.id.content);
            mTime = view.findViewById(R.id.time);
            mTag = view.findViewById(R.id.tag);
            mNeed = view.findViewById(R.id.need);
            mTitle = view.findViewById(R.id.title);
            mrecyclerView = view.findViewById(R.id.step);
//            mlinearLayout = view.findViewById(R.id.mLinearLayout);
        }
    }
}
